﻿using Api.Bll;
using Api.Model;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("_myAllowSpecificOrigins")]
    public class PostController : ControllerBase
    {
        [HttpGet("{id}")]
        public string GetPostById(int id)
        {
            string postInfo = JsonConvert.SerializeObject(BllFactory.GetPostBLL().GetPostById(id));
            return postInfo;
        }
        [HttpGet]
        public List<Post> GetPost(int id, int userId, string topicType, string postType)
        {
            Post post = new Post();
            post.postIcon = -1;
            post.time = DateTime.MinValue;
            post.text = "N/A";
            post.likeNum = -1;
            post.commentNum = -1;
            post.isVisable = true;
            post.weight = -1;
            post.id = id;
            post.userId = userId;
            post.title = "N/A";
            post.topicType = topicType;
            post.postType = postType;
            return BllFactory.GetPostBLL().GetPost(post, false);
        }



        [HttpPost]
        //返回1表示等待管理员审核，0表示用户不存在，-1表示用户被禁言，-2表示由于其他原因添加失败
        public int InsertPost(Post post)
        {
            post.time = DateTime.Now;

            return BllFactory.GetPostBLL().InsertPost(post);
        }
        [HttpPut]
        public bool UpdatePost(Post post)
        {
            return BllFactory.GetPostBLL().UpdatePost(post);
        }
        [HttpDelete]
        public bool DeletePost(int pid)
        {
            return BllFactory.GetPostBLL().DeletePost(pid);
        }

        [HttpPost("like")]
        public int likePost(int postId)
        {
            return BllFactory.GetPostBLL().likePost(postId);

        }

        [HttpPost("dislike")]
        public int dislike(int postId)
        {
            return BllFactory.GetPostBLL().dislikePost(postId);

        }
    }
}
